package com.example.erhuo.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.erhuo.R;
import com.example.erhuo.api.NetAPI;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MyInformationActivity extends AppCompatActivity implements View.OnClickListener {
    private String token;
    private Button exitButton;
    private Button commitButton;
    private EditText editWechat;
    private EditText editQQ;
    private EditText editPhone;

    private static String TAG = "MyInformationActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setCustomView(R.layout.title_layout);
            TextView textView = (TextView) actionBar.getCustomView().findViewById(R.id.display_title);
            textView.setText("我的信息");
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        Intent intent = getIntent();
        token = intent.getStringExtra("token");
        setContentView(R.layout.activity_my_information);
        initActivity();
        setActivity();
    }

    private void initActivity() {
        exitButton = findViewById(R.id.myInformation_button_signOut);
        commitButton = findViewById(R.id.information_button_commit);
        editWechat = findViewById(R.id.information_edit_wechat);
        editQQ = findViewById(R.id.information_edit_qq);
        editPhone = findViewById(R.id.information_edit_phone);
    }

    private void setActivity() {
        exitButton.setOnClickListener(this);
        commitButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.myInformation_button_signOut: {
                //退出登录
                Callback callback = new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                    }
                };
                NetAPI.get("api/logout/", callback, token);
                MainActivity.token = null;
                Intent intent = new Intent(MyInformationActivity.this, LoginAndRegisterActivity.class);
                startActivity(intent);
                this.finish();
                break;
            }
            case R.id.information_button_commit: {
                String wechat = editWechat.getText().toString();
                String qq = editQQ.getText().toString();
                String phone = editPhone.getText().toString();
                if (isAllEmpty(wechat, qq, phone)) {
                    Toast.makeText(getApplicationContext(), "请填写至少一个信息", Toast.LENGTH_SHORT).show();
                    return;
                }
                RequestBody body = new FormBody.Builder()
                        .add("qq", qq)
                        .add("wechat", wechat)
                        .add("phone", phone)
                        .build();
                Callback callback = new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "修改信息失败，请检查网络", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String information = response.body().string();
                        Log.i(TAG, information);
                        JSONObject json = null;
                        try {
                            json = new JSONObject(information);
                            String code = json.getString("code");
                            switch (code) {
                                case "200":
                                    Log.i(TAG, "修改成功");
                                    AlertDialog.Builder builder2 = new AlertDialog.Builder(MyInformationActivity.this);
                                    builder2.setTitle("修改成功");
                                    builder2.setMessage("现在对方可以更方便联系你啦");
                                    builder2.setPositiveButton("好的", null);
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            builder2.show();
                                        }
                                    });
                                    break;
                                default:
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(), "修改信息失败", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                    break;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                NetAPI.postJsonWithToken("api/setUserInfo/", body, callback, MainActivity.token);
                break;
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private boolean isAllEmpty(String s1, String s2, String s3) {
        return isEmpty(s1) && isEmpty(s2) && isEmpty(s3);
    }

    private boolean isEmpty(String s) {
        return s == null || s.equals("");
    }
}